/**
 * 创建一个代理环境，用于拦截对象的各种操作
 * @param {Object} target - 目标对象
 * @param {Object} options - 配置选项
 * @returns {Proxy} - 代理对象
 */
function createProxyEnvironment(target, options = {}) {
    // 基础的代理处理器
    const handler = {
        // 拦截属性读取
        get(target, property, receiver) {
            if (options.onGet) {
                options.onGet(target, property);
            }

            // 获取原始值
            const value = Reflect.get(target, property, receiver);

            // 如果属性是一个对象，则递归创建代理
            if (typeof value === 'object' && value !== null && !options.skipNestedProxies) {
                return createProxyEnvironment(value, options);
            }

            return value;
        },

        // 拦截属性设置
        set(target, property, value, receiver) {
            if (options.onSet) {
                const result = options.onSet(target, property, value);
                // 如果onSet返回false，则阻止设置
                if (result === false) {
                    return false;
                }
            }

            return Reflect.set(target, property, value, receiver);
        },

        // 拦截属性删除
        deleteProperty(target, property) {
            if (options.onDelete) {
                options.onDelete(target, property);
            }

            return Reflect.deleteProperty(target, property);
        },

        // 拦截has操作符
        has(target, property) {
            if (options.onHas) {
                options.onHas(target, property);
            }

            return Reflect.has(target, property);
        },

        // 拦截Object.getOwnPropertyNames()、Object.getOwnPropertySymbols()、Object.keys()等
        ownKeys(target) {
            if (options.onOwnKeys) {
                options.onOwnKeys(target);
            }

            return Reflect.ownKeys(target);
        },

        // 拦截Object.defineProperty()
        defineProperty(target, property, descriptor) {
            if (options.onDefineProperty) {
                options.onDefineProperty(target, property, descriptor);
            }

            return Reflect.defineProperty(target, property, descriptor);
        },

        // 拦截Object.getPrototypeOf()
        getPrototypeOf(target) {
            if (options.onGetPrototypeOf) {
                options.onGetPrototypeOf(target);
            }

            return Reflect.getPrototypeOf(target);
        },

        // 拦截Object.setPrototypeOf()
        setPrototypeOf(target, prototype) {
            if (options.onSetPrototypeOf) {
                options.onSetPrototypeOf(target, prototype);
            }

            return Reflect.setPrototypeOf(target, prototype);
        },

        // 拦截Object.isExtensible()
        isExtensible(target) {
            if (options.onIsExtensible) {
                options.onIsExtensible(target);
            }

            return Reflect.isExtensible(target);
        },

        // 拦截Object.preventExtensions()
        preventExtensions(target) {
            if (options.onPreventExtensions) {
                options.onPreventExtensions(target);
            }

            return Reflect.preventExtensions(target);
        },

        // 拦截函数调用
        apply(target, thisArg, argumentsList) {
            if (typeof target !== 'function') {
                return target;
            }

            if (options.onApply) {
                options.onApply(target, thisArg, argumentsList);
            }

            return Reflect.apply(target, thisArg, argumentsList);
        },

        // 拦截new操作符
        construct(target, argumentsList, newTarget) {
            if (options.onConstruct) {
                options.onConstruct(target, argumentsList, newTarget);
            }

            return Reflect.construct(target, argumentsList, newTarget);
        }
    };

    return new Proxy(target, handler);
}

// 使用示例
const originalObject = {
    name: 'John',
    age: 30,
    address: {
        city: 'New York',
        street: '123 Main St'
    },
    greet() {
        return `Hello, my name is ${this.name}`;
    }
};

// 创建代理对象
const proxiedObject = createProxyEnvironment(originalObject, {
    onGet(target, property) {
        console.log(`Getting property "${property}"`);
    },
    onSet(target, property, value) {
        console.log(`Setting property "${property}" to "${value}"`);
        // 可以添加验证逻辑
        if (property === 'age' && typeof value !== 'number') {
            console.error('Age must be a number');
            return false;
        }
        return true;
    },
    onApply(target, thisArg, argumentsList) {
        console.log(`Function ${target.name} called with arguments:`, argumentsList);
    }
});

// 测试属性访问
console.log(proxiedObject.name); // 会触发onGet

// 测试属性设置
proxiedObject.age = 31; // 会触发onSet
proxiedObject.age = 'thirty'; // 会触发onSet并打印错误

// 测试嵌套对象
console.log(proxiedObject.address.city); // 会触发onGet（嵌套对象也被代理）

// 测试函数调用
console.log(proxiedObject.greet()); // 会触发onApply